//
//  LastedMessageViewController.m
//  NewApp
//
//  Created by  on 12-7-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LastedMessageViewController.h"
#import "ProductData.h"
#import "NAStyledTableViewCell.h"
#import "SystemInlineViewController.h"

@interface LastedMessageViewController ()

@end

@implementation LastedMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	
    [self.tableView setFrame:CGRectMake(0.f, 25.f, 320.f, 367 - 25.f)];
    [self loadRequest];
}

- (void) dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark - Public methods

- (void) viewDetailsWithID:(NSString *) aID {
    self.title = @"返回";
//    DigitNoteInfoViewController *viewCtrl = [[DigitNoteInfoViewController alloc] init];
//    viewCtrl.ID = aID;
//    [self.navigationController pushViewController:viewCtrl animated:YES];
//    [viewCtrl release];
}

- (void) loadRequest {
    [self.engine productInfosWithPage:1];
}

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = (NSDictionary *) object;
        [result retain];
        
        self.titleLabel.text = [result objectForKey:@"top_title"];
        NSArray *thumbnails = [[result objectForKey:kData] retain];
        NSMutableArray *thumbnailImages = [[NSMutableArray alloc] init];
        NSString *style = [result objectForKey:kStyle];        
        if ([style isEqualToString:kStyleA]) {

            for (NSDictionary *dic in thumbnails) {
                NAImage *product = [[NAImage alloc] initWithDictionary:dic];
                [thumbnailImages addObject:product];
            }
            
            [self.galleryView setGalleryWithLargeImages:thumbnailImages];
            
            if (self.galleryView.superview == nil) {
                [self.view addSubview:self.galleryView];
            }
            
            [thumbnailImages release];
            
        }else {
//            for (NSDictionary *dic in thumbnails) {
//                ProductEntity *product = [[ProductEntity alloc] initWithDictionary:dic];
//                [thumbnailImages addObject:product];
//            }
//            viewCtrlB = [[ProductList_BCD_ViewController alloc] init];
//            viewCtrlB.bHideBack = NO;
//            viewCtrlB.titleText = [result objectForKey:@"top_title"];
//            viewCtrlB.dataArray = [[NSMutableArray alloc] initWithArray:thumbnailImages];
//            viewCtrlB.listStyle = [style retain];
//            self.title = @"返回";
//            [viewCtrlB.view setFrame:CGRectMake(0, 26, 320, 480)];
//            //[self.navigationController pushViewController:viewCtrlB animated:NO];
//            self.view = viewCtrlB.view;
//            [viewCtrlB reloadInputViews];
            if ([result objectForKey:kData] != nil) {
                for (NSDictionary *dic in thumbnails) {
                    ProductEntity *product = [[ProductEntity alloc] initWithDictionary:dic];
                    [self.datasourceObjects addObject:product];
                    }            
            }
            
            if (self.tableView.superview == nil) {
                [self.view addSubview:self.tableView];
            }
            [self.tableView reloadData];
            
        }
        
        [thumbnailImages release];
        [thumbnails release];
        [result release];
    }
}

#pragma mark -
#pragma mark - UITableView delegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SystemInlineViewController *vctrl = [[SystemInlineViewController alloc] init];
    vctrl.ID = ((ProductEntity *)[self.datasourceObjects objectAtIndex:indexPath.row]).ID;
    self.title = @"返回";
    [self.navigationController pushViewController:vctrl animated:YES];
    
}

#pragma mark -
#pragma mark - UITableView datasource

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datasourceObjects.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView 
          cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cellidentifier";
    NAStyledTableViewCell *cell = (NAStyledTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[NAStyledTableViewCell alloc] initWithNAStyle:self.currentStyle reuseIdentifier:cellIdentifier];
    }
    
    ProductEntity *entity = [self.datasourceObjects objectAtIndex:indexPath.row];
    cell.sID = entity.ID;
    cell.name = entity.title;
    cell.createAt = entity.sd;
    cell.info = entity.info;
    cell.imageURL = [entity.img absoluteString];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.showsAccessory = ([self.currentStyle isEqualToString:kStyleD]) ? YES : NO;
    
    cell.backgroundImage = (indexPath.row %2 == 0) ? [UIImage imageNamed:@"news_list_B_item_bg_1.png"] : [UIImage imageNamed:@"news_list_B_item_bg_2.png"];    
    [cell update];
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90.f;
}


@end
